package com.shenbianyun.bosskgpay.service.pay;



import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.pay.PaymentReqModel;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description //Jmeter 批量付款 接口
 * 不可固定业务参数
 **/

public class TestPaymentServiceJOlineSBH extends AbstractJavaSamplerClient {


    public final String CHARSET_UTF_8 = "UTF-8";
    public final Long MER_ID = 1623945517108708L;
    public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
    public final String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
    public final String INTER_KEY = "81937178188417411099C6D074DA7F3C";
    public final Long PROJECT_ID = 1610278112283252321L;
    public final String API_VERSION = "V1.0";
    public final String OMS_TEST_URL = "http://api.merchant.serviceshare.com/clientapi/clientBusiness/common";
    public  RequestMessage rm;
    public  PaymentReqModel paymentReqModel;
    public  PaymentReqModel.PayItems payItems;
    public  List payItemsList;


    public  String runTestPaymentServiceJ() throws Exception {

        rm = new RequestMessage();
        //生成guid码 理论上不重复
//        String ReqIdrandomGUID = new RandomGUID().toString();
//        rm.setReqId("test" + ReqIdrandomGUID);
        rm.setReqId("OnlineAPI" + System.currentTimeMillis() + 1);
        rm.setFunCode("6001");
        rm.setMerId(MER_ID);
        rm.setVersion(API_VERSION);



        /**--------————————————业务请求参数reqData——————————————————**/
        paymentReqModel = new PaymentReqModel();
        //生成guid码 理论上不重复
//        String BatchIdrandomGUID = new RandomGUID().toString();
        //商户批次号
//        paymentReqModel.setMerBatchId(BatchIdrandomGUID.substring(0, 30));
        paymentReqModel.setMerBatchId(System.currentTimeMillis() +""+Math.random());
        //服务商id
        paymentReqModel.setProviderId(34156L);
        //任务id（项目id）
        paymentReqModel.setTaskId(PROJECT_ID);
        //批量付款payItems字段
        payItemsList = new ArrayList();
        payItems = new PaymentReqModel.PayItems();
        //收款人名称
        payItems.setPayeeName("商海鹏");
        //身份证号
        payItems.setIdCard("22050219940425101X");
        //收款人账号
        payItems.setPayeeAcc("6214830195875009");
        //金额（分）
        payItems.setAmt(10L);
        //收款人手机号
        payItems.setMobile("18504358958");
        //备注
        payItems.setMemo("api请求贷");
        //商户订单号，同批次内确保唯一不可重复。
        payItems.setMerOrderId("order" + System.currentTimeMillis() + 1);
        //付款方式 0：银行卡，1：支付宝，2：微信
        payItems.setPaymentType(0);
        /**异步通知地址**/
        //payItems.setNotifyUrl("http://192.168.40.78:8081/pay/notify");

        payItemsList.add(payItems);
        paymentReqModel.setPayItems(payItemsList);

        String json = JsonUtils.toJson(paymentReqModel);
//        System.out.println("请求未加密数据:" + json);

        //加密处理
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));

        String reqStr = JsonUtils.toJson(rm);
//        System.out.println("请求数据：" + reqStr);

        //发送请求
        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
//        System.out.println("响应数据：" + resData);


        return resData;
    }





    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            resData = runTestPaymentServiceJ();

            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("resCode").equals("0000")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + "", SampleResult.TEXT);
                System.out.println("批量付款结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("批量付款结果:" + false + " " + jsonObject1.get("resMsg"));
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }
}
